<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="{{ url_for('static', filename= 'css/style.css') }}">
    <title>个人博客</title>
</head>
<body>
    {% extends "base.html" %}
    {% block content %}

    <div class="mb-3" style="margin-top: 15px;">
        {% if current_user.is_authenticated %}
            <a href="{{ url_for('auth.logout') }}" class="btn custom-outline-gray mr-2">退出</a>
            <a href="{{ url_for('new') }}" class="btn custom-outline-gray mr-2">发布</a>
        {% else %}
            <a href="{{ url_for('auth.login') }}" class="btn custom-outline-gray mr-2">登录</a>
        {% endif %}
    </div>

    <div class="row">
        {% for post in posts %}
        <div class="col-md-6">
            <a href="{{ url_for('edit', id=post['id']) }}">
                <h3>{{ post['title'] }}</h3>
            </a>
        </div>
        <div class="col-md-6">
            <span class="badge badge-primary mr-2">{{ post['created'] }}</span>
            {% if current_user.is_authenticated %}
            <a href="{{ url_for('edit', id=post['id']) }}" class="badge badge-warning">
                编辑
            </a>
            <!-- 使用 a 标签模拟删除操作 -->
             <a href="{{ url_for('delete', id=post['id']) }}" class="badge badge-warning"
               onclick="if(confirm('确定要删除吗？')) {
                   event.preventDefault();
                   document.getElementById('delete-form-{{ post['id'] }}').submit();
               } else {
               event.preventDefault();
               location.reload();
               }">
               删除
            </a>
            <!-- 隐藏的表单，点击确定，隐藏的表单会被提交 -->
            <form id="delete-form-{{ post['id'] }}" action="{{ url_for('delete', id=post['id']) }}" method="post" style="display: none;">
                <input type="hidden" name="_method" value="DELETE">
            </form>
            {% endif %}
        </div>
        {% endfor %}
    </div>
    {% endblock %}
</body>
</html>